behave xterm

" variable setup

set hid "keep undo history when changing between buffers
set wildmenu " nicer filename autocomplete - like CTRL+P
set wildmode=longest,full
set nocompatible
set modeline    " read mode line
set expandtab
set shiftwidth=4	" insert 4 spaces when pressing [TAB]
set softtabstop=4	" insert 4 spaces when pressing [TAB]
set tabstop=8	        " view \t as 8 space wide, for viewing code with tabs (evil)
set ruler
set textwidth=0 "this sets it for plain text files.  It is reset by filetype below
set showmatch
set smartindent
set visualbell
"set shell=/usr/local/bin/bash
set showfulltag
set incsearch
set hlsearch
set undolevels=1000 "explicitly state default, just in case...

set tags=tags,$mainline/tags

set showcmd             " display incomplete commands
set backspace=indent,eol,start
set ignorecase          " ignore case in search patterns ...
set smartcase           " ... unless pattern contains uppercase

"set title titlestring=%t%M%R%([%{Tlist_Get_Tagname_By_Line()}]%)\ (%F)
"set statusline=%t%m%r%h%w%y\ %([%{Tlist_Get_Tagname_By_Line()}]%)\ (%F)%=%l,%c%V\ (%3b=0x%02B)\ %P
set scrolloff=3
set incsearch           " do incremental searching
"set listchars=tab:^.,trail:·

" c indent: 
" (0 -- line up the next line within open parentheses with the first
"       non-whitespace character.
" l1 -- line up with 'case' statement labels correctly
set cino=(0,l1


" abbreviations
" note that the odd number of colons works because you enter a third
" colon to trigger the abbreviation, which becomes the second after 
" expansion
abbr as:: ActiveState:
abbr pm:: PureMessage:



" keymappings

map X :!/usr/bin/xterm -ls -sb -rightbar -sl 10000 -display $DISPLAY -title 'xterm' & <CR><CR>
map ,m :w <CR> :make <CR>
map ,b :! links 
map <F5> :vert diffsplit

" p4 diff is done in the if_gui section
"map ,a :!p4 annotate "%"<CR><CR>
map ,e :!p4 edit "%"<CR><CR>
map ,l :!p4 filelog "%"<CR>



" TagList management
let Tlist_Auto_Open = 0
let Tlist_Process_File_Always = 1
set updatetime=1500
map ,t :Tlist<CR>

map Q :nohlsearch<CR>

"set cinwords=        " do I want to zero this setting???





" perl buffer, using a temp file, into a new window
function! PerlOutput()
    let l:tmpfile1 = tempname()
    let l:tmpfile2 = tempname()
    
    execute "normal:w!" . l:tmpfile1 . "\<CR>"
    execute "normal:! perl ".l:tmpfile1." \> ".l:tmpfile2." 2\>\&1 \<CR>"
    execute "normal:new\<CR>"
    execute "normal:edit " . l:tmpfile2 . "\<CR>"
endfunction





"Only do this part when compiled with support for autocommands.
if has("autocmd")
  " Remove all cprog autocommands
  au!
  filetype plugin indent on

  " Turn off line wrap for common files
  au BufNewFile,BufRead db.*	setlocal nowrap
  au BufNewFile,BufRead /etc/*	setlocal nowrap

  " Filetype declaration for mutt-ng
  au BufNewFile,BufRead muttng-*-\w\+,muttng\w\{6\},ae\d setf mail

  au BufNewFile,BufRead,StdinReadPost *
    \ let s:l1 = getline(1) |
    \ if s:l1 =~ '^Return-Path: ' |
    \   setf mail |
    \ endif

  " When starting to edit a file:
  "   For C and C++ files set formatting of comments and set C-indenting on.
  "   For other files switch it off.
  "   Don't change the order, it's important that the line with * comes first.
 augroup Text
  autocmd FileType *      setlocal formatoptions=tcq comments&
  autocmd FileType crontab setlocal tw=0
  autocmd FileType c,cpp  setlocal tw=79 formatoptions=croq cindent comments=sr:/*,mb:*,el:*/,://
  au FileType c setlocal cino=:0,g0,t0,(0,w1,W4
  au FileType c setlocal expandtab
  autocmd FileType tex  setlocal tw=80 comments=:%
  autocmd FileType vim  setlocal tw=80 comments=:\"
  autocmd FileType mail setlocal formatoptions=tcq tw=70 comments&
  autocmd FileType make setlocal formatoptions=tcq tw=0 ts=4 noet comments=:#

  autocmd FileType perl setlocal formatoptions=croq comments=:# tw=80
  autocmd FileType perl setlocal errorformat=%f:%l:%m 
  autocmd FileType perl setlocal autowrite 
  autocmd FileType perl map ,p :call PerlOutput()<CR>
  autocmd FileType perl map ,c :"%"!perltidy -q -pt=2 -sak="if elsif" <CR>
 
  autocmd FileType xml setlocal sts=2 sw=2 et tw=0
  
  autocmd FileType awk  setlocal formatoptions=croq autoindent comments=:# 
  autocmd FileType sh  setlocal formatoptions=croq autoindent comments=:# 
  autocmd FileType tex  setlocal formatoptions=trcq ai comments=:% et 
  
  " for editing mail in Mutt
  au FileType mail setlocal tw=62
  au FileType mail map to gg/^To<CR>A
  au FileType mail map cc gg/^Cc<CR>A
  au FileType mail map su gg/^Subject<CR>A
  au FileType mail map Bo gg}o
  au FileType mail map ,s dGo-- :r ~/.signature <CR>
  " mail headers (for OutHouse)
  au FileType mail map ,I  gg}OImportance: high<CR>X-Priority: 1<ESC>
  au FileType mail map ,i  gg}OImportance: low<CR>X-Priority: 5<ESC>
  au FileType mail map ,m  gg}OX-Message-Flag: 
 
  au FileType html setlocal tw=0

  " since there's so much html directly in the php. Boo!
  au FileType php setlocal tw=0
  
  au FileType ruby setlocal sw=2
  au FileType ruby setlocal ts=2
  
  " cuz cory said so!
  au FileType js setlocal tw=0

  " vim calls our templates 'smarty'  ???
  au FileType smarty setlocal tw=0
  
  au FileType p4 setlocal tw=0
  au FileType p4 setlocal wrap
  au FileType p4 setlocal ai      
  au FileType p4 setlocal noet 

  " unless we hear otherwise, *.t is perl
  autocmd BufNewFile,BufRead *.t setf perl


 syntax on
  "autocmd FileType c,cpp map  :syntax region BraceFold start="{" end="}" transparent fold <CR> zR
  autocmd FileType c,cpp syn sync fromstart
"  autocmd FileType c,cpp set foldmethod=syntax
 augroup END

	    " vim -b : edit binary using xxd-format!
" augroup Binary
"    au BufReadPre  *.bin,*.exe,*.ld,*.ftp,*.out,*.lib let &binary=1
"    au BufReadPost * if &binary | "%"!xxd -g 4
"    au BufReadPost * so $VIMRUNTIME/syntax/xxd.vim | set filetype=xxd | endif
"    au BufWritePre * if &binary | "%"!xxd -r
"    au BufWritePre * endif
" augroup END


" After entering a buffer, the working directory is changed to the directory
" where the file in the current buffer comes from.  Can be confusing til you get
" used to it  :)
if $vim_cd
    autocmd BufEnter * :lcd %:p:h
endif

  augroup GPGASCII
     au!
     au BufReadPost *.asc  :"%"!gpg -q -d
     au BufReadPost *.asc  |redraw
     au BufWritePre *.asc  :"%"!gpg -q -e -a
     au BufWritePost *.asc u
     au VimLeave *.asc :!clear
  augroup END 

endif " has("autocmd")



":set diffopt+=iwhite


" mode options
if has("gui_running")
    colorscheme slate
    set laststatus=1 
    set guioptions=aeirmT
    set mouse=nvi
    map ,d :!P4DIFF="gvimdiff -f -R" p4 diff "%" <CR><CR>
    map ,w :!P4DIFF="gvimdiff -f -R -c 'set diffopt+=iwhite'" p4 diff "%" <CR><CR>
    " diff setup mapping
    map ,D :set guioptions=agimrb <CR> :let &columns = ((&columns*2 > 182)? 182: &columns*2)<CR> :browse vert diffsplit .<CR>
    let Tlist_Show_Menu = 1
else
    map ,d :!P4DIFF="vimdiff -f -R" p4 diff "%" <CR><CR>
    map ,w :!P4DIFF="vimdiff -f -R -c 'set diffopt+=iwhite'" p4 diff "%" <CR><CR>
    set background=dark
    set laststatus=2 
    set t_Co=256
    
    colorscheme zion

    if has("mouse")
        set mouse=
    endif
endif "has ("gui_running")

" If comparing files side-by-side, then ...
if &diff
    " double the width up to a reasonable maximum
    let &columns = ((&columns*2 > 172)? 172: &columns*2)

	" add bottom scrollbar
	set guioptions=agimrb
endif 


syntax enable

" fold options
"map ,Z :syntax region BraceFold start="{" end="}" transparent fold <CR> zR
syn sync fromstart
"set foldmethod=syntax
let perl_include_pod=1
let perl_extended_vars=1

" apparently we need to re-run this after the folding config prep
syntax on

map <F2> <ESC>:call P4WhoDunnit()<CR>
function! P4WhoDunnit()
    execute '!p4 annotate -i -q "%" | head -n' . line (".") . '| tail -n1 | cut -f 1 -d: | xargs p4 describe -s |grep -B99 "^Affected files..."|grep -v "^Affected files..."'
endfunction

map <F4> :!p4 sync "%"<ESC>:set noro
map <F5> :!p4 edit "%"<ESC>:set noro
map <F6> :!p4 revert "%"<ESC>:set ro
map <F7> :!p4 add "%"<ESC>:set noro
map <F12> :set columns=80<CR>




